<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>用户：</label>
            <el-select
              filterable
              clearable
              v-model="userId"
              placeholder="请选择用户">
              <el-option
                v-for="(item,index) in userOption"
                :key="index"
                :label="item.username"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>商品：</label>
            <el-select
              filterable
              clearable
              v-model="productId"
              placeholder="请选择商品">
              <el-option
                v-for="(item,index) in productOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <span @click="getTableInfo(1)" class="operate_btn search_btn">查询</span>
            <span @click="resetTable" class="operate_btn reset_btn">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span @click="openPopup('')" class="operate_btn search_btn" v-if="btn.add">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          :data="tableInfo"
          :header-cell-style="tableHeader"
          border
          ref="multipleTable"
          style="width: 100%;"
          tooltip-effect="dark"
          v-loading="loading">
          <el-table-column
            label="ID"
            prop="id"
            width="80">
          </el-table-column>
          <el-table-column
            label="更新时间"
            prop="modifyTime"
            sortable
            width="160">
            <template slot-scope="scope">
              <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop="productName">
          </el-table-column>
          <el-table-column
            label="商品图片">
            <template slot-scope="scope">
              <img :src="scope.row.productPhoto" alt="tzs" class="icon" v-if="scope.row.productPhoto">
              <p v-else>——</p>
            </template>
          </el-table-column>
          <el-table-column
            label="账号"
            prop="username">
          </el-table-column>
          <el-table-column
            label="用户名"
            prop="username">
            <template slot-scope="scope">
              <span>{{ scope.row.name ? scope.row.name : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="价格(元)">
            <template slot-scope="scope">
              <span>{{ scope.row.price ? scope.row.price : 0 }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            width="100" v-if="!(btn && btn.freeze)">
            <template slot-scope="scope">
                    <span :class="{'unusual':scope.row.enable===0}"
                          class="normal">{{ scope.row.enable ? '可用' : '禁用' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            prop="2"
            sortable="custom"
            width="100" v-if="btn && btn.freeze">
            <template slot-scope="scope">
              <div>
                <el-switch
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="freezeItem(scope.row,scope.$index)"
                  v-model="scope.row.enable">
                </el-switch>
                <span class="normal" v-if="scope.row.enable===1">启用</span>
                <span class="unusual" v-else>禁用</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            v-if="btn&&btn.update"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span @click="openPopup(scope.row.id)" class="change_info" v-if="btn.update">编辑</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[15, 20, 30, 40]"
          :total="totals"
          @current-change="getTableInfo"
          @size-change="sizeChange"
          background
          layout="prev, pager, next, sizes,jumper">
        </el-pagination>
      </div>
    </div>
    <Edit :mid="menuId" @listenToChild="listenToEdit" v-if="isPop"></Edit>

  </div>
</template>

<script>
import Edit from "./edit";
import commons from "../../../utils/common";
import {getBtn} from "../../../utils/btnAuth";
import {listData, updateProductStatus} from "@/api/config/userProduct";
import {listAllData} from "@/api/config/product";
import {getAllUser} from "@/api/sys/user";

export default {
    name: 'rolePrice',
    components: {
      Edit
    },
    data() {
      return {
        name: '',
        username:'',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo: [],
        menuId: '',
        warehouseData: {},
        recoveryData:{},
        isPop: false,
        isComm: false,
        isRec:false,
        btn: {},
        userOption:[],
        productOption:[],
        productId:'',
        userId:''
      }
    },
    mounted() {
      this.btn = getBtn();
      if (this.btn.query) {
        this.getTableInfo(1);
        this.getAllUser();
        this.getProductList();
      }
    },
    methods: {
      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit: that.pageSize,
          page: page,
          // productName: that.name,
          // username:that.username,
          userId:that.userId,
          productId:that.productId
        };
        listData(queryParams).then(res => {
          that.tableInfo = res.list ? res.list : [];
          that.totals = res.count;
          that.loading = false;
        }).catch((e) => {
          that.tableInfo = [];
          that.totals = 0;
          that.loading = false;
        });
      },
      //用户列表
      getAllUser(){
        let that = this;
        getAllUser('').then(res=>{
          that.userOption = res ? res: [];
        }).catch((e)=>{
          that.userOption = [];
        })
      },
      //获取商品
      getProductList() {
        listAllData({display:1}).then(res=>{
          this.productOption = res ? res: [];
        }).catch((e)=>{
          this.productOption = [];
        })
      },
      resetTable() {
        this.name = '';
        this.username='';
        this.pageSize = 15;
        this.userId = '';
        this.productId = '';
        this.getTableInfo(1)
      },

      // 移除
      freezeItem(obj,index) {
        let that = this;
        that.$confirm('确定'+(obj.enable?'启用':'禁用')+ obj.username +'的「' + obj.productName + '」?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let queryParams = {
            id: obj.id,
            enable:obj.enable
          };
          updateProductStatus(queryParams).then(res => {
            that.getTableInfo(that.currentPage);
            that.$message.success({
              message:'修改成功！',
              duration:1500
            });
          }).catch((e) => {
          });
        }).catch(() => {
          //重置
          that.tableInfo[index].enable = obj.enable == 0 ? 1:0;
        });
      },

      // 打开弹窗
      openPopup(id) {
        this.isPop = true;
        this.menuId = id;
      },

      openCommodity(obj) {
        this.isComm = true;
        this.warehouseData = obj
      },

      openRecovery(obj){
        this.isRec = true;
        this.recoveryData = obj
      },

      listenToEdit(e) {
        this.isPop = false;
        this.isComm = false;
        this.isRec=false;
        if (e.refresh) {
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time) {
        return commons.formatTime(time)
      },

      sizeChange(val) {
        this.pageSize = val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
      },
    }
  }
</script>

<style scoped>

</style>
